package com.google.codejam.alienlanguage;

import com.google.codejam.core.test.ACodeJamTestCase;

import java.util.List;

/**
 * Concrete implementation of the <code>ACodeJamTestCase</code> for the
 * "Alien Language" problem.
 */
public class AlienLanguageTestCase extends ACodeJamTestCase
{
	private String regex;
	private List<String> list_of_words;
	private int result;

	/**
	 * Constructor that passes a reference to the list of words required to solve
	 * this test case.
	 *
	 * @param list_of_words <code>List&lt;String&gt;</code> reference containing
	 * the list of words.
	 */
	public AlienLanguageTestCase(List<String> list_of_words)
	{
		this.list_of_words = list_of_words;
	}

	@Override
	public void parseDataString(String data_str)
	{
		regex = fixRegexParenthesis(data_str);
	}

	@Override
	public void solve()
	{
		int match_counter = 0;

		for (String instance : list_of_words)
		{
			if (instance.matches(regex))
			{
				match_counter++;
			}
		}

		result = match_counter;
	}

	@Override
	public String getResultString()
	{
		return super.getResultString() + result;
	}

	/**
	 * Replaces '(' with '[' and ')' with ']' to ensure the passed String is
	 * returned in a form compatible with Java regex.
	 *
	 * @param input <code>String</code> to be 'fixed'.
	 * @return Regex compatible <code>String</code>. Returns <code>null</code>
	 * if passed value (<code>input</code>) is <code>null</code>.
	 */
	private String fixRegexParenthesis(String input)
	{
		if (input == null)
		{
			return null;
		}

		return input.replace('(', '[').replace(')', ']');
	}
}
